﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using JobDescOnline.Presentation.Models;
using eq.CoreBase.View;
using eq.CoreBase.Control;

namespace JobDescOnline.Presentation.Controllers
{
    public class JobDescRelationShipController : Controller
    {
        public JobDescDBEntities1 db = new JobDescDBEntities1();

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    JobDescRelationShip jobRelation = new JobDescRelationShip
                    {
                        Rowstatus = true,
                        JobDescriptionID = Convert.ToInt32(collection["JobDescriptionID"]),
                        Department = collection["Department"],
                        Purpose = collection["Purpose"],
                        TypeRelation = collection["TypeRelation"],
                        CreatedBy = UserAuthenticated.isAuthenticated ? UserAuthenticated.authUser.FirstName : "DEFAULT",
                        CreatedOn = DateTime.Now
                    };

                    db.JobDescRelationShips.AddObject(jobRelation);
                    db.SaveChanges();
                }

                int id = Convert.ToInt32(collection["JobDescriptionID"]);
                string TypeRelation = collection["TypeRelation"];
                var model = db.JobDescRelationShips.Where(d => d.JobDescriptionID == id && d.TypeRelation == TypeRelation);
                return View(model);
            }
            catch
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    JobDescRelationShip jobrelation = db.JobDescRelationShips.First(d => d.ID == id);
                    jobrelation.Rowstatus = true;
                    jobrelation.JobDescriptionID = Convert.ToInt32(collection["JobDescriptionID"]);
                    jobrelation.Department = collection["Department"];
                    jobrelation.Purpose = collection["Purpose"];
                    jobrelation.TypeRelation = collection["TypeRelation"];
                    jobrelation.LastModifiedBy = UserAuthenticated.isAuthenticated ? UserAuthenticated.authUser.FirstName : "DEFAULT";
                    jobrelation.LastModifiedOn = DateTime.Now;

                    db.JobDescRelationShips.ApplyCurrentValues(jobrelation);
                    db.SaveChanges();
                }

                int ID = Convert.ToInt32(collection["JobDescriptionID"]);
                string TypeRelation = collection["TypeRelation"];
                var model = db.JobDescRelationShips.Where(d => d.JobDescriptionID == ID && d.TypeRelation == TypeRelation);
                return View(model);
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            JobDescRelationShip jobrelation = db.JobDescRelationShips.Single(d => d.ID == id);
            int ID = Convert.ToInt32(jobrelation.JobDescriptionID);
            string type = jobrelation.TypeRelation;
            db.JobDescRelationShips.DeleteObject(jobrelation);

            var model = db.JobDescRelationShips.Where(d => d.JobDescriptionID == ID && d.TypeRelation == type);
            db.SaveChanges();
            return View(model);
        }
    }
}
